home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 2 / CU Amiga Magazine's Super CD-ROM 02 (1996)(EMAP Images)(GB)[!][issue 1996-04].iso / magazine / amiga_e / jrh-rkrm-partone / intuition / io_methods / closewindowsafely.e next >
Text File  |  1995-03-08  |  2KB  |  59 lines

  1. -> closeWindowSafely
  2. ->
  3. -> These functions close an Intuition window that shares a port with other
  4. -> Intuition windows.
  5. ->
  6. -> We are careful to set the UserPort to NIL before closing, and to free any
  7. -> messages that it might have been sent.
  8.  
  9. OPT MODULE  -> E-Note: create a module for re-use
  10.  
  11. MODULE 'exec/lists',
  12.        'exec/nodes',
  13.        'exec/ports',
  14.        'intuition/intuition'
  15.  
  16. -> Function to remove and reply all IntuiMessages on a port that have been
  17. -> sent to a particular window (note that we don't rely on the succ pointer
  18. -> of a message after we have replied it)
  19. PROC stripIntuiMessages(mp:PTR TO mp, win)
  20.   DEF msg:PTR TO intuimessage, succ
  21.   msg:=mp.msglist.head
  22.   WHILE succ:=msg.execmessage.ln.succ
  23.     IF msg.idcmpwindow=win
  24.       -> Intuition is about to free this message.
  25.       -> Make sure that we have politely sent it back.
  26.       Remove(msg)
  27.       ReplyMsg(msg)
  28.     ENDIF
  29.     msg:=succ
  30.   ENDWHILE
  31. ENDPROC
  32.  
  33. -> Entry point to closeWindowSafely().
  34. -> Strip all IntuiMessages from an IDCMP which are waiting for a specific
  35. -> window.  When the messages are gone, set the UserPort of the window to NIL
  36. -> and call ModifyIDCMP(win,0).  This will free the Intuition parts of the IDCMP
  37. -> and turn off messages to this port without changing the original UserPort
  38. -> (which may be in use by other windows).
  39. -> E-Note: this is the function we want to export from the module
  40. EXPORT PROC closeWindowSafely(win:PTR TO window)
  41.   -> We forbid here to keep out of race conditions with Intuition
  42.   Forbid()
  43.  
  44.   -> Send back any messages for this window  that have not yet been processed
  45.   stripIntuiMessages(win.userport, win)
  46.  
  47.   -> Clear UserPort so Intuition will not free it
  48.   win.userport:=NIL
  49.  
  50.   -> Tell Intuition to stop sending more messages
  51.   ModifyIDCMP(win, 0)
  52.  
  53.   -> Turn multitasking back on
  54.   Permit()
  55.  
  56.   -> Now it's safe to really close the window
  57.   CloseWindow(win)
  58. ENDPROC
  59.